package design.pattern.application.CommandAndChain;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @program JavaBase
 * @description: 命令对象 ls -l /usr 命令名、参数、操作数
 * @author: zhanglu
 * @create: 2020-05-22 14:32:00
 */
public class CommandVo {

    //定义参数名与参数的分割符号，一般是空格
    public final static String DIVIDE_FLAG = " ";
    //定义参数前的符号，Unix一般是-，如ls -la
    public final static String PREFIX = "-";
    //命令名，如 ls、du
    private String commandName;
    //参数列表
    private List<String> paramList = new ArrayList<>();
    //操作数列表
    private List<String> dataList = new ArrayList<>();
    //通过构造函数传递命令
    public CommandVo(String commandStr){
        if(commandStr != null && commandStr.length() != 0){
            String[] complexStr = commandStr.split(CommandVo.DIVIDE_FLAG);
            this.commandName = complexStr[0];
            //把参数放到List
            for (int i = 0; i < complexStr.length; i++) {
                String str = complexStr[i];
                //包含前缀符号，认为是参数
                if(str.indexOf(CommandVo.PREFIX) == 0){
                    this.paramList.add(str.replace(CommandVo.PREFIX, "").trim());
                }else {
                    this.dataList.add(str.trim());
                }
            }
        }else {
            System.out.println("命令为空");
        }
    }
    //得到命令
    public String getCommandName(){
        return this.commandName;
    }
    //获得参数
    public List<String> getParam(){
        if(this.paramList.size() == 0){
            //为了方便处理空参数
            this.paramList.add("");
        }
        return new ArrayList<>(new HashSet<>(this.paramList));
    }
    //获得操作数
    public List<String> getData(){
        return this.dataList;
    }

}
